package com.study.controller;

import com.study.common.ErrorCode;
import com.study.common.RestfulResult;
import com.study.entity.BlogUser;
import com.study.request.BlogReqDto;
import com.study.service.ContentFeignService;
import com.vedeng.study.model.dto.BlogDTO;
import com.vedeng.study.model.dto.BlogDetailDTO;
import com.vedeng.study.request.BlogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.lang.management.MonitorInfo;
import java.util.List;

@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private ContentFeignService contentFeignService;

    @RequestMapping("/toAddBlog")
    public ModelAndView toAddFriend(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/user/test");
        return modelAndView;
    }

    @RequestMapping("/toEditBlog")
    public ModelAndView toEditBlog(BlogReqDto blogReqDto){
        //查询博客详情
        BlogVO blogVO = new BlogVO();
        blogVO.setBlogid(blogReqDto.getBlogid());
        BlogDetailDTO blogDetailDTO = contentFeignService.blogDetail(blogVO);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/user/edit");
        modelAndView.addObject("blogVO",blogDetailDTO.getBlogVO());
        return modelAndView;
    }

    @PostMapping("/addBlog")
    public ModelAndView insertBlog(BlogReqDto blogReqDto, HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        BlogUser userInfo = (BlogUser) request.getSession().getAttribute("userInfo");
        BlogVO blogVO = new BlogVO();
        blogVO.setBlogauthor(userInfo.getId().toString());
        blogVO.setBlogtitle(blogReqDto.getBlogtitle());
        blogVO.setBlogContent(blogReqDto.getBlogContent());
        int i = contentFeignService.insertBlog(blogVO);
        if (i == 1){
            List<BlogDTO> blogList = getBlogList(userInfo);
            modelAndView.setViewName("/page/user/list");
            modelAndView.addObject("blogDTOList",blogList);
        }else {
            modelAndView.addObject("errorMessage", ErrorCode.FAIL_CODE.getMessage());
            modelAndView.setViewName("/page/user/error");
        }
        return modelAndView;
    }


    @PostMapping("/updateBlog")
    public ModelAndView updateBlog(BlogReqDto blogReqDto,HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        BlogUser userInfo = (BlogUser) request.getSession().getAttribute("userInfo");
        BlogVO blogVO = new BlogVO();
        blogVO.setBlogid(blogReqDto.getBlogid());
        blogVO.setBlogauthor(userInfo.getId().toString());
        blogVO.setBlogtitle(blogReqDto.getBlogtitle());
        blogVO.setBlogContent(blogReqDto.getBlogContent());
        int i = contentFeignService.updateBlog(blogVO);
        if (i == 1){
            List<BlogDTO> blogList = getBlogList(userInfo);
            modelAndView.setViewName("/page/user/list");
            modelAndView.addObject("blogDTOList",blogList);
        }else {
            modelAndView.addObject("errorMessage", ErrorCode.FAIL_CODE.getMessage());
            modelAndView.setViewName("/page/user/error");
        }
        return modelAndView;
    }

    @RequestMapping("/blogDetail")
    public ModelAndView blogDetail(BlogReqDto blogReqDto){
        //查询博客详情
        BlogVO blogVO = new BlogVO();
        blogVO.setBlogid(blogReqDto.getBlogid());
        BlogDetailDTO blogDetailDTO = contentFeignService.blogDetail(blogVO);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/user/detail");
        modelAndView.addObject("blogVO",blogDetailDTO.getBlogVO());
        modelAndView.addObject("commentDTOList",blogDetailDTO.getCommentDTOList());
        return modelAndView;
    }

    @RequestMapping("/deleteBlog")
    public ModelAndView deleteBlog(BlogReqDto blogReqDto,HttpServletRequest request){
        BlogVO blogVO = new BlogVO();
        blogVO.setBlogid(blogReqDto.getBlogid());
        int i = contentFeignService.deleteBlog(blogVO);
        ModelAndView modelAndView = new ModelAndView();
        BlogUser userInfo = (BlogUser) request.getSession().getAttribute("userInfo");
        if (i == 1){
            List<BlogDTO> blogList = getBlogList(userInfo);
            modelAndView.setViewName("/page/user/list");
            modelAndView.addObject("blogDTOList",blogList);
        }else {
            modelAndView.addObject("errorMessage", ErrorCode.FAIL_CODE.getMessage());
            modelAndView.setViewName("/page/user/error");
        }
        return modelAndView;
    }

    @RequestMapping("/addComments")
    public ModelAndView addComments(BlogReqDto blogReqDto,HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/page/user/detail");
        BlogVO blogVO = new BlogVO();
        BlogUser userInfo = (BlogUser) request.getSession().getAttribute("userInfo");
        blogVO.setBlogid(blogReqDto.getBlogid());
        blogVO.setComment(blogReqDto.getComment());
        blogVO.setUserName(userInfo.getUsername());
        blogVO.setBlogauthor(userInfo.getId().toString());
        String comments = contentFeignService.addComments(blogVO);
        if ("3s内只允许评论一次".equals(comments)){
            modelAndView.addObject("error",comments);
        }
        BlogVO blogVO1 = new BlogVO();
        blogVO1.setBlogid(blogReqDto.getBlogid());
        BlogDetailDTO blogDetailDTO = contentFeignService.blogDetail(blogVO1);
        modelAndView.addObject("blogVO",blogDetailDTO.getBlogVO());
        modelAndView.addObject("commentDTOList",blogDetailDTO.getCommentDTOList());
        return modelAndView;
    }




    private List<BlogDTO> getBlogList(BlogUser userInfo) {
        //查询列表页
        BlogVO blogVO = new BlogVO();
        blogVO.setBlogauthor(userInfo.getId().toString());
        blogVO.setUserName(userInfo.getUsername());
        return contentFeignService.blogList(blogVO);
    }

}
